{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Building Neural Network from Scratch\n",
    "\n",
    "Putting all the concepts we have learned so far, we will see how to build a neural network\n",
    "from scratch. We will learn how the neural network learns to perform the XOR gate\n",
    "operation. The XOR gate returns 1 only when exactly only one of its inputs is 1 else it returns 0 as shown in\n",
    "the following figure:\n",
    "\n",
    "![image](images/1.png)\n",
    "\n",
    "\n",
    "To perform the XOR gate operation, we build a simple two-layer neural network as shown\n",
    "in the following figure. As you can observe, we have an input layer with two nodes, a\n",
    "hidden layer with five nodes and an output layers which consist of 1 node:\n",
    "\n",
    "\n",
    "![image](images/2.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, import the libraries:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Prepare the data as shown in the above XOR table:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = np.array([ [0, 1], [1, 0], [1, 1],[0, 0] ])\n",
    "y = np.array([ [1], [1], [0], [0]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define the number of nodes in each layer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "num_input = 2\n",
    "num_hidden = 5\n",
    "num_output = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Initialize weights and bias randomly. First, we initialize, input to hidden layer weights:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Wxh = np.random.randn(num_input,num_hidden)\n",
    "bh = np.zeros((1,num_hidden))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now initialize, hidden to output layer weights:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Why = np.random.randn (num_hidden,num_output)\n",
    "by = np.zeros((1,num_output))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define the sigmoid activation function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def sigmoid(z):\n",
    "    return 1 / (1+np.exp(-z))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define the derivative of the sigmoid function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def sigmoid_derivative(z):\n",
    "    return np.exp(-z)/((1+np.exp(-z))**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define the forward propagation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def forward_prop(x,Wxh,Why):\n",
    "    z1 = np.dot(x,Wxh) + bh\n",
    "    a1 = sigmoid(z1)\n",
    "    z2 = np.dot(a1,Why) + by\n",
    "    y_hat = sigmoid(z2)\n",
    "    \n",
    "    return z1,a1,z2,y_hat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define the backward propagation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def backword_prop(y_hat, z1, a1, z2):\n",
    "    delta2 = np.multiply(-(y-y_hat),sigmoid_derivative(z2))\n",
    "    dJ_dWhy = np.dot(a1.T, delta2)\n",
    "    delta1 = np.dot(delta2,Why.T)*sigmoid_derivative(z1)\n",
    "    dJ_dWxh = np.dot(x.T, delta1) \n",
    "\n",
    "    return dJ_dWxh, dJ_dWhy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define the cost function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def cost_function(y, y_hat):\n",
    "    J = 0.5*sum((y-y_hat)**2)\n",
    "    \n",
    "    return J"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set the learning rate and number of training iterations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "alpha = 0.01\n",
    "num_iterations = 5000"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's start training the network:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cost = []\n",
    "for i in range(num_iterations):\n",
    "    \n",
    "    #perform forward propagation and predict output\n",
    "    z1,a1,z2,y_hat = forward_prop(x,Wxh,Why)\n",
    "    \n",
    "    #perform backward propagation and calculate gradients\n",
    "    dJ_dWxh, dJ_dWhy = backword_prop(y_hat, z1, a1, z2)\n",
    "        \n",
    "    #update the weights\n",
    "    Wxh = Wxh -alpha * dJ_dWxh\n",
    "    Why = Why -alpha * dJ_dWhy\n",
    "    \n",
    "    #compute cost\n",
    "    c = cost_function(y, y_hat)\n",
    "    \n",
    "    #store the cost\n",
    "    cost.append(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plot the cost function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0,0.5,'Cost')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgo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ggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.grid()\n",
    "plt.plot(range(num_iterations),cost)\n",
    "\n",
    "plt.title('Cost Function')\n",
    "plt.xlabel('Training Iterations')\n",
    "plt.ylabel('Cost')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can notice, the loss decreases over the training iterations. \n",
    "\n",
    "\n",
    "Thus, we have learned how to build a neural network from scratch in the next chapter we will one of the popularly used deep learning libraries called TensorFlow. "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
